<template>
	<view class="content">
		<view class="form" v-if="!info || info.status == 2">
			<u--form ref="uForm" errorType="none" :rules="rules" :model="form" labelPosition="top" labelWidth="80" :labelStyle="{color: '#ffffff', fontSize: '25rpx', paddingBottom: '10rpx'}">
				<u-form-item label="姓名" prop="username">
					<u--input class="input" v-model="form.username" color="#ffffff" maxlength="20"></u--input>
				</u-form-item>
				<u-form-item label="身份证号" prop="idcard">
					<u--input class="input" v-model="form.idcard" color="#ffffff" maxlength="40"></u--input>
				</u-form-item>
				<u-form-item>
					<button class="light-btn btn" @click="onClick">立即认证</button>
				</u-form-item>
			</u--form>
		</view>
		<view class="card" v-if="info">
			<view class="row">
				<text class="label b">实名认证</text>
				<text class="value b">{{info?info.status:'0' | userAuthStatus}}</text>
			</view>
			<view class="row">
				<text class="label">姓名:</text>
				<text class="value">{{info ? info.username : ''}}</text>
			</view>
			<view class="row no">
				<text class="label">身份证号:</text>
				<text class="value">{{info ? info.idcard : '' | desensitized(4, 10)}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
	export default {
		components: {
		},
		data() {
			return {
				info: {},
				form: {
					id: null,
					username: '',
					idcard: '',
					front: ' ',
					contrary: ' '
				},
				rules: {
					'username': {
						type: 'string',
						required: true,
						message: '请输入姓名',
						trigger: ['change']
					},
					'idcard': [
						{
							required: true,
							message: '请输入身份证',
							trigger: ['change']
						}, 
						{
							type: 'number',
							message: '身份证不正确',
							trigger: ['change']
						}
					]
				},
			}
		},
		onLoad() {
			this.getInfo()
		},
		methods: {
			...mapActions('user', ['updateUserAuth', 'getUserAuthInfo', 'applyUserAuth']),
			getInfo(){
				this.getUserAuthInfo().then(res => {
					this.info = res.data
				})
			},
			onClick(){
				this.$refs.uForm.validate().then(res => {
					if(!this.info){
						this.applyUserAuth(this.form).then(r => {
							uni.$u.toast('提交成功')
							this.getInfo()
						})
					} else {
						this.form.id = this.info.id
						this.updateUserAuth(this.form).then(r => {
							uni.$u.toast('提交成功')
							this.getInfo()
						})
					}
				}).catch(errors => {
					uni.$u.toast(errors[0].message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		.form{
			padding: 20rpx 20rpx;
			.input{
				background-color: #232639 !important;
				border: 1rpx solid #666666 !important;
				color: #ffffff;
			}
		}
		.btn{
			margin-top: 20rpx;
			width: 50%;
		}
	}
	.card{
		background-color: #232639;
		border-radius: 10rpx;
		padding: 0rpx 30rpx;
		margin: 20rpx 20rpx;
		.no{
			border: none !important;
		}
		.row{
			display: flex;
			justify-content: space-between;
			border-bottom: 1rpx solid #505050;
			padding: 30rpx 0;
			color: #999999;
			
			.b{
				font-weight: bold;
				color: #ffffff;
			}
		}
	}
</style>
